var mongoose = require("mongoose");
var Schema = mongoose.Schema;
var type_id = require("./type");
var ObjectId = Schema.Types.ObjectId;
var CaseSchema = new Schema({
    id: {
        type: Number,
        default: 0
    },
    title: String,
    remark: String,
    address: String,
    price: String,
    appraisal: String,
    tag: String,
    typeId: {
        type: Number
    },
    numberPeople: {
        type: Number
    },
    commentId: {
        type: ObjectId,
        ref: 'comment'
    },
    dataLink: String,
    imgPath: String,
    isIndex: {
        type: Boolean,
        default: false
    },
    intro: {
        type: String
    },
    guide: {
        type: String
    },
    publish: {
        type: Date,
        default: Date.now
    }
});
//使用findByIdAndUpdate进行ID自增，将返回值重新插入留言表里
CaseSchema.pre('save', function (next) {
    var doc = this;
    type_id.findByIdAndUpdate({_id: 'ids'}, {$inc: {"ids.pid": 1}}, function (error, counter) {
        if (error)
            return next(error);
        console.log(counter.ids)
        doc.id = counter.ids.pid;
        next();
    });
});

var Cases = mongoose.model('cases', CaseSchema, 'cases');

module.exports = Cases;